package CommonEvent;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.By;

import java.util.List;
import java.util.Random;
import static Utils.MyLogger.logInfo;

/**
 * @author 锄禾
 * @projectName MkPack
 * @description: 自定义Monkey Xpath点击事件类
 */
public class MonkeyXpathEvent extends MonkeyEvent{
    private AndroidDriver androidDriver;
    private By xpath0 = By.xpath("");
    private By xpath1 = By.xpath("");   // 自定义
    private By xpath2 = By.xpath("");   // 自定义

    public MonkeyXpathEvent(AndroidDriver driver){
        super(MonkeyEvent.EVENT_TYPE_CONTENT);
        androidDriver = driver;
    }

    public int injectEvent(){
        return 0;
    }

    /**
    　* @author 锄禾
    　* @description: 随机点击Text控件
    　* @param
    　* @return
    　*/
    public int injectEventText(){
        try {
            Random rd = new Random();
            List eli = androidDriver.findElements(xpath0);
            if (eli.size() > 0){
                int raEl = rd.nextInt(eli.size());
                AndroidElement androidElement = (AndroidElement) eli.get(raEl);
                androidElement.click();
                logInfo(String.format(this.getClass().getName() +
                        "-" + "随机点击Text控件事件：%s", raEl));
                return MonkeyEvent.INJECT_SUCCESS;
            }else {
                logInfo(this.getClass().getName() + "-" + "随机点击Text控件事件未命中！");
                return MonkeyEvent.INJECT_FIAL;
            }
        } catch (Exception e) {
            //e.printStackTrace();
            logInfo(this.getClass().getName() + "-" + "随机点击Text控件事件异常：" + e);
            return MonkeyEvent.INJECT_FIAL;
        }
    }

    /**
    　* @author 锄禾
    　* @description: 随机点击图标事件
    　* @param
    　* @return
    　*/
    public int injectEventImage(){
        try {
            logInfo("操作图标控件开始！");
            Random rd = new Random();
            List eli = androidDriver.findElements(xpath1);
            if (eli.size() > 0){
                int rdE = rd.nextInt(eli.size() + 1);
                AndroidElement androidElement = (AndroidElement) eli.get(rdE);
                androidElement.click();
                logInfo(String.format(this.getClass().getName() +
                        "-" + "随机点击图标控件事件：%s!", rdE));
                return MonkeyEvent.INJECT_SUCCESS;
            }else {
                logInfo(this.getClass().getName() + "-" + "随机点击图标控件事件未命中！");
                return MonkeyEvent.INJECT_FIAL;
            }
        } catch (Exception e) {
            //e.printStackTrace();
            logInfo(this.getClass().getName() + "-" + "随机点击图标控件事件异常：" + e);
            return MonkeyEvent.INJECT_FIAL;
        }
    }
}
